<template>
    <view class="manage_info_components contract_region">
        <div class="pub_input">
            <div class="li">
                <div class="title"><text class="xing">*</text> 方案名称</div>
                <div class="input">
                    <input class="inp" type="text" v-model="form.name" placeholder="请输入" placeholder-style="color:#bbb">
                </div>
            </div>
            <div class="li">
                <div class="title"><text class="xing">*</text> 预警余额线(元)</div>
                <div class="input">
                    <input class="inp" type="digit" @input="dealPrice(1)" v-model="form.warn_balance" placeholder="请输入"
                        placeholder-style="color:#bbb">
                </div>
            </div>
            <div class="li">
                <div class="title"><text class="xing">*</text> 跳闸时长(分)</div>
                <div class="input input_right">
                    <picker :value="form.method_key" :range="options.warnMethod" range-key="name"
                        @change="(data) => pickerChange('method', data)">
                        <input class="inp" type="text" :value="form.method_name" placeholder="请选择"
                            placeholder-style="color:#bbb" disabled>
                        <image class="right_icon" mode="aspectFit"
                            src="https://sppcp-1322907932.cos.ap-beijing.myqcloud.com/app/img_24.png" />
                    </picker>
                </div>
            </div>
            <div class="li">
                <div class="title"> 备注</div>
                <div class="input">
                    <input class="inp" type="text" v-model="form.remark" placeholder="请输入" placeholder-style="color:#bbb">
                </div>
            </div>
        </div>
        <div class="rules">
            <div class="p_1">解释说明</div>
            <div class="p_2">1、预警余额线：房间余额低于设置的报警余额时，通知提醒+跳闸（根据设置的跳闸时长）</div>
            <div class="p_2">2、跳闸时长：房间余额低于报警余额时，设置不跳闸、跳闸xx分钟后自动合闸</div>
        </div>
        <div class="pub_btns" v-if="editId == ''" :style="{ 'bottom': bottom }">
            <div class="pub_btn pub_btn_big_normal" @click="toSubmit">保存并提交</div>
        </div>
        <div class="pub_btns" v-else :style="{ 'bottom': bottom }">
            <div class="pub_btn_normal pub_btn_normal_border pub_btn_small" @click="toDelete">删除</div>
            <div class="pub_btn pub_btn_small" @click="toSubmit">保存并提交</div>
        </div>
    </view>
</template>

<script>
export default {
    props: {
        editId: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            bottom: `calc(${getApp().globalData.statusBottomHeight}px + 32rpx)`,
            options: {
                warnMethod: [],
            },
            form: {
                name: '',
                warn_balance: '',
                method: '',
                method_name: '',
                method_key: '',
                remark: '',
            }
        };
    },
    watch: {
        editId: {
            handler() {
                if (this.editId) this.getDetail()
            },
            immediate: true,
            deep: true
        },
    },
    onLoad() { },
    mounted() {
        this.getWarnMethod();
    },
    methods: {
        dealPrice(type, key) {
            // console.log(this.form.fixedCharge.unitPrice);usageThreshold
            let value;
            if (type == 1) value = this.form.warn_balance;
            // value = value.replace(/[^\d.]/g, "");//清除"数字"和"."以外的字符    
            value = value.replace(/^\./g, "");//验证第一个字符是数字而不是字符    
            value = value.replace(/\.{2,}/g, ".");//只保留第一个.清除多余的    
            value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数  
            setTimeout(() => {
                if (type == 1) this.form.warn_balance = value;
                this.$forceUpdate();
            }, 1);
        },
        pickerChange(type, e) {
            // console.log(type, e.detail.value);
            if (type == 'method') {
                this.form.method_key = e.detail.value;
                this.form.method = this.options.warnMethod[e.detail.value].Id;
                this.form.method_name = this.options.warnMethod[e.detail.value].name;
            }
        },
        async getWarnMethod() {
            const that = this;
            that.$comFun.openLoading();
            const res = await that.$request.get(`/mobile/manager/warn_method`, {});
            // console.log(res);
            that.$comFun.closeLoading();
            if (res.code == 200) {
                this.options.warnMethod = res.data;
            }
        },
        async getDetail() {
            const that = this;
            that.$comFun.openLoading();
            const res = await that.$request.get(`mobile/manager/warn_scheme/${that.editId}`, {});
            // console.log(res);
            that.$comFun.closeLoading();
            if (res.code == 200) {
                console.log(this.options.warnMethod);
                this.form = {
                    name: res.data.name,
                    warn_balance: res.data.warnBalance,
                    method: res.data.warnMethodId,
                    method_name: res.data.sppcpWarnMethod.name || '',
                    method_key: '',
                    remark: res.data.remark,
                };
            }
        },
        async toSubmit() {
            const that = this;
            const form = JSON.parse(JSON.stringify(that.form));

            if (!form.name) {
                that.$comFun.toast('请填写方案名称');
                return;
            }
            if (!form.warn_balance) {
                that.$comFun.toast('请填写预警余额线');
                return;
            }
            if (!form.method) {
                that.$comFun.toast('请选择跳闸时长');
                return;
            }
            // if (!form.remark) {
            //     that.$comFun.toast('请填写备注');
            //     return;
            // }
            uni.showModal({
                title: '提示',
                content: '是否确定保存？',
                success: async function (modalRes) {
                    if (modalRes.confirm) {
                        that.$comFun.openLoading();
                        let res;
                        delete form.method_name;
                        delete form.method_key;
                        form.warn_balance = Number(form.warn_balance);
                        form.method = Number(form.method);
                        if (that.editId) {
                            res = await that.$request.put(`mobile/manager/warn_scheme/${that.editId}`, form);
                        } else {
                            res = await that.$request.post('mobile/manager/warn_scheme', form);
                        }
                        // console.log(res);
                        that.$comFun.closeLoading();
                        if (res.code == 200) {
                            that.$comFun.toast('保存成功');
                            uni.$emit(`manageListReload_2`);
                            setTimeout(() => {
                                that.$comFun.closeWin();
                            }, 1000)
                        } else {
                            that.$comFun.toast(res.msg);
                        }
                    }
                }
            });
        },
        toDelete() {
            const that = this;
            uni.showModal({
                title: '提示',
                content: '是否确定删除？',
                success: async function (modalRes) {
                    if (modalRes.confirm) {
                        that.$comFun.openLoading();
                        const res = await that.$request.delete(`mobile/manager/warn_scheme/${that.editId}`);
                        // console.log(res);
                        that.$comFun.closeLoading();
                        if (res.code == 200) {
                            that.$comFun.toast('删除成功');
                            uni.$emit(`manageListReload_2`);
                            setTimeout(() => {
                                that.$comFun.closeWin();
                            }, 1000)
                        } else {
                            that.$comFun.toast(res.msg);
                        }
                    }
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/manage.scss";
</style>
